package fr.cpe.j2ee.projet.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


/**
 * The persistent class for the conversation database table.
 * 
 */
@Entity
@NamedQuery(name="Conversation.findAll", query="SELECT c FROM Conversation c")
public class Conversation implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="con_id")
	private int conId;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="con_day_time_begin")
	private Date conDayTimeBegin;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="con_day_time_end")
	private Date conDayTimeEnd;

	//bi-directional many-to-one association to Project
	@ManyToOne
	@JoinColumn(name="pro_id")
	private Project project;

	//bi-directional many-to-one association to Message
	@OneToMany(mappedBy="conversation", fetch=FetchType.EAGER)
	@OrderBy("mesSendTime")
	private Set<Message> messages;

	//bi-directional many-to-one association to EmployeesConversation
	@OneToMany(mappedBy="conversation", fetch=FetchType.EAGER)
	private Set<EmployeesConversation> participants;

	public Conversation() {
		participants = new HashSet<EmployeesConversation>();
	}

	public int getConId() {
		return this.conId;
	}

	public void setConId(int conId) {
		this.conId = conId;
	}

	public Date getConDayTimeBegin() {
		return this.conDayTimeBegin;
	}

	public void setConDayTimeBegin(Date conDayTimeBegin) {
		this.conDayTimeBegin = conDayTimeBegin;
	}

	public Date getConDayTimeEnd() {
		return this.conDayTimeEnd;
	}

	public void setConDayTimeEnd(Date conDayTimeEnd) {
		this.conDayTimeEnd = conDayTimeEnd;
	}

	public Project getProject() {
		return this.project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public Set<Message> getMessages() {
		return this.messages;
	}

	public void setMessages(Set<Message> messages) {
		this.messages = messages;
	}

	public Message addMessage(Message message) {
		getMessages().add(message);
		message.setConversation(this);

		return message;
	}

	public Message removeMessage(Message message) {
		getMessages().remove(message);
		message.setConversation(null);

		return message;
	}

	public Set<EmployeesConversation> getParticipants() {
		return this.participants;
	}

	public void setParticipants(Set<EmployeesConversation> participants) {
		this.participants = participants;
	}

	public EmployeesConversation addParticipant(EmployeesConversation participant) {
		getParticipants().add(participant);
		participant.setConversation(this);

		return participant;
	}

	public EmployeesConversation removeParticipant(EmployeesConversation participant) {
		getParticipants().remove(participant);
		participant.setConversation(null);

		return participant;
	}
	
	public boolean isInConversation(Employee employee) {
        for(EmployeesConversation e : this.getParticipants()) {
            if(e.getEmployee().equals(employee)) {
                    return true;
            }
        }
        return false;
	}
}